package com.jeeplus.modules.file.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.action.entity.UserAction;
import com.jeeplus.modules.action.service.UserActionService;
import com.jeeplus.modules.file.entity.FileClassify;
import com.jeeplus.modules.file.service.FileClassifyService;
import com.jeeplus.modules.sys.utils.UserUtils;

/**
 * 资源共享分类管理Controller
 * @author wangkai
 * @version 2017-12-14
 */
@Controller
@RequestMapping(value = "${adminPath}/file/fileClassify")
public class FileClassifyController extends BaseController {

	@Autowired
	private FileClassifyService fileClassifyService;
	
	@Autowired
	private UserActionService userActionService;
	
	@ModelAttribute
	public FileClassify get(@RequestParam(required=false) String id) {
		FileClassify entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = fileClassifyService.get(id);
		}
		if (entity == null){
			entity = new FileClassify();
		}
		return entity;
	}
	
	/**
	 * 资源共享分类列表页面
	 */
	@RequiresPermissions("file:fileClassify:list")
	@RequestMapping(value = {"list", ""})
	public String list(FileClassify fileClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		fileClassify.setCreateBy(UserUtils.getUser());
		List<FileClassify> list = fileClassifyService.findList(fileClassify);
		model.addAttribute("list", list);
		return "modules/file/fileClassifyList";
	}
	
	/**
	 * 查看，增加，编辑资源共享分类表单页面
	 */
	@RequiresPermissions(value={"file:fileClassify:view","file:fileClassify:add","file:fileClassify:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(FileClassify fileClassify, Model model) {
		if (fileClassify.getParent()!=null && StringUtils.isNotBlank(fileClassify.getParent().getId())){
			fileClassify.setParent(fileClassifyService.get(fileClassify.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(fileClassify.getId())){
				FileClassify fileClassifyChild = new FileClassify();
				fileClassifyChild.setParent(new FileClassify(fileClassify.getParent().getId()));
				List<FileClassify> list = fileClassifyService.findList(fileClassify); 
				if (list.size() > 0){
					fileClassify.setSort(list.get(list.size()-1).getSort());
					if (fileClassify.getSort() != null){
						fileClassify.setSort(fileClassify.getSort() + 30);
					}
				}
			}
		}
		if (fileClassify.getSort() == null){
			fileClassify.setSort(30);
		}
		model.addAttribute("fileClassify", fileClassify);
		return "modules/file/fileClassifyForm";
	}

	/**
	 * 保存资源共享分类
	 */
	@RequiresPermissions(value={"file:fileClassify:add","file:fileClassify:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(FileClassify fileClassify, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, fileClassify)){
			return form(fileClassify, model);
		}
		if(!fileClassify.getIsNewRecord()){//编辑表单保存
			FileClassify t = fileClassifyService.get(fileClassify.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(fileClassify, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			fileClassifyService.save(t);//保存
		}else{//新增表单保存
			fileClassifyService.save(fileClassify);//保存
		}
		
		UserAction userAction = new UserAction();
		userAction.setAction("编辑资料分类");
		userActionService.save(userAction);
		
		addMessage(redirectAttributes, "保存资源共享分类成功");
		return "redirect:"+Global.getAdminPath()+"/file/fileClassify/?repage";
	}
	
	/**
	 * 删除资源共享分类
	 */
	@RequiresPermissions("file:fileClassify:del")
	@RequestMapping(value = "delete")
	public String delete(FileClassify fileClassify, RedirectAttributes redirectAttributes) {
		fileClassifyService.delete(fileClassify);
		addMessage(redirectAttributes, "删除资源共享分类成功");
		return "redirect:"+Global.getAdminPath()+"/file/fileClassify/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		FileClassify fileClassify = new FileClassify();
		//fileClassify.setCreateBy(UserUtils.getUser());
		List<FileClassify> list = fileClassifyService.findList(fileClassify);
		for (int i=0; i<list.size(); i++){
			FileClassify e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"select"})
	public String select(FileClassify fileClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		fileClassify.setCreateBy(UserUtils.getUser());
		List<FileClassify> list = fileClassifyService.findList(fileClassify); 
		model.addAttribute("list", list);
		return "modules/file/fileClassifySelect";
	}
	
}